<template>
	<view class="container" style="display: flex;flex-direction: column;">
		<view class="top" style="width: 100%;height: 76rpx;background-color: #FFF;">
			<u-search placeholder="请输入商品名称" v-model="itemName" @search='search' @custom='search'></u-search>
		</view>
		<!-- 占位 -->
		<view style="width: 750rpx;height: 76rpx;flex-shrink: 0;z-index: -1;"></view>
		<div style="display: flex;flex-direction: row;width: 100%;background-color: #EFEFEF;" class="content">
			<scroll-view class="left" scroll-y="true" style="position: fixed;width: 180rpx;height:100vh;background-color: #EFEFEF;display: flex;flex-direction: column;align-items: center;flex-shrink: 0;overflow-y: auto;">
				<block v-if="nodeModel.isShowForBtn1">
					<view style="display: flex;flex-direction: row;align-items: center;height: 50px;line-height: 50px;">
						<view style="width: 15px;height: 1px;background-color: #666666;"></view>
						<view style="margin: 0 5px;color: #666666;font-size: 14px;">{{nodeModel.options1}}</view>
						<view style="width: 15px;height: 1px;background-color: #666666;"></view>
					</view>
					<view style="display: flex;flex-direction: column;align-items: center;width: 100%;">
						<view v-for="(item, index) of titleForGroup1" :key='index' @click="menuAction(item)" style="height: 50px;width: 100%;line-height: 50px;text-align: center;font-size: 15px;" 
					:style="{
						'color': item.isShow?nodeModel.colorForBtn1:'#666666',
						'background-color': item.isShow?nodeModel.backgroundColorForBtn1:'#EFEFEF',
					}">{{item.title}}</view>
					</view>
				</block>
				<block v-if="nodeModel.isShowForBtn2">
					<view style="display: flex;flex-direction: row;align-items: center;height: 50px;line-height: 50px;">
						<view style="width: 15px;height: 1px;background-color: #666666;"></view>
						<view style="margin: 0 5px;color: #666666;font-size: 14px;">{{nodeModel.options2}}</view>
						<view style="width: 15px;height: 1px;background-color: #666666;"></view>
					</view>
					<view style="display: flex;flex-direction: column;align-items: center;width: 100%;">
						<view v-for="(item, index) of titleForGroup2" :key='index' @click="menuAction(item)" style="height: 50px;width: 100%;line-height: 50px;text-align: center;font-size: 15px;" 
						:style="{
							'color': item.isShow?nodeModel.colorForBtn1:'#666666',
							'background-color': item.isShow?nodeModel.backgroundColorForBtn1:'#EFEFEF',
						}">{{item.title}}</view>
					</view>
				</block>
			</scroll-view>
			
			<view style="display: flex;flex-direction: row;flex-wrap: wrap;align-content: flex-start;background-color: #f8f8f8;margin-left: 180rpx;" class="content">
				<!-- <view v-for="(item, index) in itemList" :key="index" @click="itemHandle(item)">
					<jc-iteminfo-container :nodeModel='nodeModel' :item='item'></jc-iteminfo-container>
				</view> -->
				<view v-for="(item, index) in itemList" :key='index' @click="itemHandle(item)"
				style="display: flex;flex-direction: column;margin-top: 16rpx;background-color: #FFF;width: 270rpx;margin-left: 8rpx;">
					<image :src="item.coverUrl" mode='aspectFill' style="width: 100%;height: 380rpx;"></image>
					<view style="padding: 16rpx;">
						<view style="font-size: 28rpx;" class="limitLineNumOfTwo">{{item.name}}</view>
						<view style="margin-top: 10rpx;color: #fa3534;">¥{{item.suggestSellingPrice}}</view>
					</view>
				</view>
			</view>
		</div>
	</view>
</template>

<script>
	import {
		page
	} from '@/api/page.js'
	import {
		common
	} from "@/api/common.js";
	import {
		merchant
	} from '@/api/merchant.js'
	export default {
		components: {},
		data() {
			return {
				type: '',
				page: 1,
				size: 20,
				titleForGroup1: [],
				titleForGroup2: [],
				lastItem: {},
				itemList: [],
				nodeList: [],
				nodeModel: {},
				pageConfig: {},
				selectAdvert: [{}],
				itemName: ''
			}
		},
		onLoad() {
			// this.findShopinCateList()
			// this.findActivityList()
			this.getPageByClientname()
		},
		onShow() {
			var item = this.lastItem
			if(this.nodeModel.isShowForBtn1 && this.titleForGroup1.length>0){
				this.menuAction(item)
			}else if(this.nodeModel.isShowForBtn2 && this.titleForGroup2.length>0){
				this.menuAction(item)
			}
		},
		onPullDownRefresh() {
			this.page = 1
			this.getItemList()
		},
		onReachBottom() {
			console.log('上拉加载更多')
			if (this.page == 0) {
				return
			}
			this.getItemList()
		},
		onShareAppMessage(res) {
			return {
				title: '',
				path: this.$Route.path
			}
		},
		methods: {
			getPageByClientname() {
				page.getPageByClientname({
					name: this.$Route.name
				}).then(res => {
					uni.stopPullDownRefresh()
					if (res.data.code == 10000) {
						this.pageConfig = res.data.data
						this.setPageConfig(this.pageConfig)
						this.nodeList = res.data.data.nodeList
						for (var node of this.nodeList) {
							if (node.componentModel.name == 'itemCate') {
								this.nodeModel = node
								for(var item of this.nodeModel.advertList){
									item.isShow = false
									if(item.types == 'group1'){
										this.titleForGroup1.push(item)
									}else if(item.types == 'group2'){
										this.titleForGroup2.push(item)
									}
								}
								if(this.nodeModel.isShowForBtn1 && this.titleForGroup1.length>0){
									this.menuAction(this.titleForGroup1[0])
								}else if(this.nodeModel.isShowForBtn2 && this.titleForGroup2.length>0){
									this.menuAction(this.titleForGroup2[0])
								}
							}
						}
					}
				})
			},
			setPageConfig(pageConfig) {
				this.pageConfig = pageConfig
				uni.setNavigationBarTitle({
					title: this.pageConfig.title
				})
				uni.setNavigationBarColor({
					backgroundColor: this.pageConfig.background,
					frontColor: (!this.pageConfig.navigationBarBackground || this.pageConfig.navigationBarBackground == '#FFF' ||
						this.pageConfig.navigationBarBackground == '#fff' || this.pageConfig.navigationBarBackground == '#FFFFFF' ||
						this.pageConfig.navigationBarBackground == '#ffffff') ? "" : "#ffffff"
				})
			},
			menuAction(item){
				console.log('点击菜单', item)
				this.lastItem.isShow = false
				item.isShow = true
				this.lastItem = item
				
				
				this.page = 1
				this.getItemList()
			},
			search(){
				this.page = 1
				this.getItemList()
			},
			getItemList() {
				var query = ''
				if(this.lastItem.query){
					query = JSON.parse(this.lastItem.query)
				}
				var params = {
					page: this.page,
					size: this.size,
					name: this.itemName,
				}
				// if(!this.lastItem.query) return
				params = Object.assign(query, params) //当前页面展示的列表
				merchant.getItemListForC(params).then(res => {
					uni.stopPullDownRefresh();
					if (res.data.code == 10000) {
						if (this.page == 1) {
							this.itemList = []
						}
						var list = res.data.data.list
						this.itemList = [...this.itemList, ...list]
						if (res.data.data.next) {
							this.page += 1
						} else {
							this.page = 0
						}
					} else {
						this.itemList = []
					}
				})
			},
			itemHandle(item) {
				console.log('热门商品点击事件', item)
				this.$Router.push({
					name: 'itemDetail',
					params: {
						itemId: item.id,
						type: 'item'
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		width: 750rpx;
		height: 100%;
	}
	.top{
		position: fixed;
		left: 0;
		top: 0;
		z-index: 999;
	}
	.content{
		height: calc(100vh - 78rpx);
	}

</style>
